package com.example.jsoupdemo.util;

import com.example.jsoupdemo.beans.ProxyIp;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 生成动态ip工具类
 */
public class ProxyIpUtil {
    final static Logger logger = LoggerFactory.getLogger(ProxyIpUtil.class);
    public static ProxyIp getProxyIp(){
        String url ="http://http.tiqu.alicdns.com/getip3?num=1&type=1&pro=&city=0&yys=0&port=1&pack=88530&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions=&gm=4";
        ProxyIp proxyIp = new ProxyIp();
        //直接获取到文本
        try {
            String ipStr = Jsoup.connect(url).get().text();
            String ip = ipStr.split(":")[0];
            String port = ipStr.split(":")[1];
            //logger.info(ipStr+"----"+ip+"---"+port);
            proxyIp.setIp(ip);
            proxyIp.setPort(port);
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("出现异常>>>>>>>>两秒后继续生成动态ip");
            try {
                Thread.sleep(2000); //休眠2秒
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            getProxyIp();
        }
        return proxyIp;
    }
}
